﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Text;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.Domain.Repositories;
using YAPACS.BusinessObjects.Managements.Devices;
using YAPACS.BusinessObjects.Managements.TextTemplates;
using YAPACS.Organizations;
using YAPACS.RisServer.Abstractions;

namespace YAPACS.RisServer.Controllers;

[Authorize]
[ApiController]
public class QueueController(
    IExamQueueService examQueueService,
    IRepository<TextTemplate, Guid> textTemplateRepository,
    IRepository<Department, Guid> departmentRepository,
    IRepository<Station, Guid> stationRepository)
    : AbpControllerBase
{
    private readonly IExamQueueService _examQueueService = examQueueService;
    private readonly IRepository<TextTemplate, Guid> _textTemplateRepository = textTemplateRepository;
    private readonly IRepository<Department, Guid> _departmentRepository = departmentRepository;
    private readonly IRepository<Station, Guid> _stationRepository = stationRepository;

    [AllowAnonymous]
    [HttpGet("api/queue/get-waiting-queues/department/{departmentCode}")]
    public async Task<IActionResult> GetDepartmentWaitingQueues(string departmentCode)
    {
        var result = await _examQueueService.GetDepartmentWaitingQueuesAsync(departmentCode);
        return Ok(result);
    }

    [AllowAnonymous]
    [HttpGet("api/queue/get-calling-queues/department/{departmentCode}")]
    public async Task<IActionResult> GetDepartmentCallingQueues(string departmentCode)
    {
        var result = await _examQueueService.GetDepartmentCallingQueuesAsync(departmentCode);
        return Ok(result);
    }

    [AllowAnonymous]
    [HttpGet("api/queue/get-waiting-queues/station/{stationCode}")]
    public async Task<IActionResult> GetStationWaitingQueues(string stationCode)
    {
        var result = await _examQueueService.GetStationWaitingQueuesAsync(stationCode);
        return Ok(result);
    }

    [AllowAnonymous]
    [HttpGet("api/queue/get-calling-queues/station/{stationCode}")]
    public async Task<IActionResult> GetStationCallingQueues(string stationCode)
    {
        var result = await _examQueueService.GetStationCallingQueuesAsync(stationCode);
        return Ok(result);
    }

    [AllowAnonymous]
    [HttpPost("api/queue/remove-calling-queue/{callingQueueId}")]
    public async Task<IActionResult> RemoveCallingQueue(Guid callingQueueId)
    {
        await _examQueueService.RemoveCallingQueueAsync(callingQueueId);
        return Ok();
    }

    [AllowAnonymous]
    [HttpGet("queue/department/{departmentCode}")]
    public async Task<IActionResult> GetDepartmentTextTemplateAsync(string departmentCode, [FromQuery] string name)
    {
        string content = string.Empty;
        try
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                //获取默认资源
                content = await Extensions.GetResourceStringAsync("Resources.CallQueue.department-waiting-queue.html");
            }
            else
            {
                var department = await _departmentRepository.GetAsync(e => e.Code == departmentCode);
                var textTemplate = await _textTemplateRepository.GetAsync(e => e.Department == department && e.Name == name);
                content = textTemplate.Content;
            }
            return Content(content, contentType: "text/html; charset=utf-8", Encoding.UTF8);
        }
        catch
        {
            content = await Extensions.GetResourceStringAsync("Resources.CallQueue.bad.html");
            //返回错误页面
            return Content(content, contentType: "text/html; charset=utf-8", Encoding.UTF8);
        }
    }

    [AllowAnonymous]
    [HttpGet("queue/station/{stationCode}")]
    public async Task<IActionResult> GetStationTextTemplateAsync(string stationCode, [FromQuery] string name)
    {
        string content = string.Empty;
        try
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                //获取默认资源
                content = await Extensions.GetResourceStringAsync("Resources.CallQueue.station-waiting-queue.html");
            }
            else
            {
                var station = await _stationRepository.GetAsync(e => e.Code == stationCode);
                var textTemplate = await _textTemplateRepository.GetAsync(e => e.Department == station.Room.Department && e.Name == name);
                content = textTemplate.Content;
            }
            return Content(content, contentType: "text/html; charset=utf-8", Encoding.UTF8);
        }
        catch
        {
            content = await Extensions.GetResourceStringAsync("Resources.CallQueue.bad.html");
            //返回错误页面
            return Content(content, contentType: "text/html; charset=utf-8", Encoding.UTF8);
        }
    }
}
